<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/25
 * Time: 3:57 PM
 */

namespace App\BO\OrderStatus;

use App\Entities\OrderEntity;
use App\Entities\OrderModel;

trait OrderJudgeStateTrait
{
    /**
     * @return OrderEntity
     */
    private function getOrder()
    {
        return $this->order;
    }

    /**
     * 是否等待初始化
     * @return bool
     */
    public function isWaitingInit()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_BEFORE_INIT;
    }

    /**
     * 订单是否为待支付状态
     *
     * @return bool
     */
    public function isPending()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_WAITING_PAY   // 新判断条件
            || (!$this->getOrder()->isPaid()
                && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE
                && !$this->getOrder()->getClosed()); // 老的判断条件，兼容老数据
    }

    /**
     * 订单是否待分配
     * @return bool
     */
    public function isDistributing()
    {
        return  $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_WAITING_DISTRIBUTION   // 新判断条件
            || ($this->getOrder()->isPaid()
            && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE
            && $this->getOrder()->getShipStatus() == OrderModel::SHIP_STATUS_DISTRIBUTION
            && $this->getOrder()->getRefundStatus() != OrderModel::REFUND_STATUS_SUCCESS);
    }

    /**
     * 待上门服务
     * @return bool
     */
    public function isWaitingService()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_WAITING_SERVICE   // 新判断条件
            || ($this->getOrder()->isPaid()
            && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE
            && $this->getOrder()->getShipStatus() == OrderModel::SHIP_STATUS_PENDING
            && $this->getOrder()->getRefundStatus() != OrderModel::REFUND_STATUS_SUCCESS);
    }

    /**
     * 退款申请后台审核中
     * @return bool
     */
    public function isRefundWaitingCheck()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUND_WAITING_CHECK;
    }

    /**
     * 退款等待第三方异步通知结果
     * @return bool
     */
    public function isRefundWaitingThirdPartNotify()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUND_WAITING_THIRD_PART_NOTIFY;
    }

    /**
     * 服务中
     * @return bool
     */
    public function isServicing()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_SERVICING;
    }

    /**
     * 退款中
     * @return bool
     */
    public function isRefunding()
    {
        return  $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUNDING   // 新判断条件
            || ($this->getOrder()->getRefundStatus() == Order::REFUND_STATUS_APPLIED
                && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE);
    }

    /**
     * 是否待评价
     * @return bool
     */
    public function isWaitingComment()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_WAITING_COMMENT   // 新判断条件
            || (($this->getOrder()->getShipStatus() == Order::SHIP_STATUS_DELIVERED)
                && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE
                && !$this->getOrder()->reviewed());
    }


    /**
     * 退款是否被管理员拒绝
     * @return bool
     */
    public function isRefundRejectedByAdmin()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUND_REJECT_BY_ADMIN;
    }

    /**
     * 用户取消退款申请
     * @return bool
     */
    public function isRefundCancelledByUser()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUND_CANCELLED_BY_USER;
    }

    /**
     * 末支付，已经完成
     * @return bool
     */
    public function isNotPaidAndFinished()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_NOT_PAID_NAD_FINISHED;
    }

    /**
     * 末支付，可派单(后台导入订单)
     * @return bool
     */
    public function isPendingCouldDispatch()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_WAITING_PAY_COULD_DISPATCH;
    }

    /**
     * 已评价
     * @return bool
     */
    public function reviewed()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REVIEWED
            || ($this->getOrder()->reviewed()
                && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE);
    }

    /**
     * 退款失败
     * @return bool
     */
    public function isRefundFailed()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUND_FAILED;
    }

    /**
     * 退款成功
     * @return bool
     */
    public function isRefundSuccess()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REFUND_SUCCESS;
    }

    /**
     * 过期末支付，订单关闭
     * @return bool
     */
    public function isExpireClosed()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_EXPIRED
            || (!$this->getOrder()->isPaid()
                && $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_DEFAULT_VALUE
                &&  $this->getOrder()->getClosed());
    }

    /**
     * 是否为已经报备
     */
    public function isReported()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_REPORTED;
    }

    /**
     * 是否为用户取消
     */
    public function isUserCanceled()
    {
        return $this->getOrder()->getOrderState() == OrderStateDefinition::STATE_USER_CANCEL;
    }

    /**
     * 订单是否已经关闭
     * @return bool
     */
    public function isClosed()
    {
        return $this->getOrder()->getClosed();
    }
}
